import { Command } from "commander";
import { ethers } from "ethers";
import { BridgeToken__factory } from "@peersyst/xrp-evm-contracts/dist/typechain/factories/BridgeToken__factory";
import { Log, LogStatus, LogType } from "../util/Logger";

const program = new Command("mint-tokens")
    .option("-o, --owner-seed <value>", "Owner seed")
    .option("-t, --token-address <value>", "Token address")
    .option("-a, --amount <value>", "Token code")
    .option("-d, --destination-address <value>", "Destination address")
    .option("-n, --node <value>", "The network node", "https://rpc-evm-sidechain.peersyst.tech")
    .action(async ({ node, ownerSeed, amount, tokenAddress, destinationAddress }) => {
        if (node && ownerSeed && amount && tokenAddress) {
            try {
                const provider = new ethers.providers.JsonRpcProvider(node);
                const signerWallet = new ethers.Wallet(ownerSeed, provider);
                const bridgeTokenFactory = new BridgeToken__factory(signerWallet);
                const bridgeToken = bridgeTokenFactory.attach(tokenAddress);

                const dest = destinationAddress || signerWallet.address;

                Log(LogType.Tool, LogStatus.ToDo, `Minting ${amount} tokens to address ${dest}...`);
                const parsedAmount = ethers.utils.parseEther(amount.toString());
                await (await bridgeToken.mint(dest, parsedAmount)).wait();
                Log(LogType.Tool, LogStatus.Done, `Minted ${amount} tokens to address ${dest} successfully`);
            } catch (error) {
                console.log(error);
                process.exit(1);
            }
            process.exit(0);
        } else {
            program.help();
            process.exit(1);
        }
    });
export default program;
